<?php
/*
//  Copyright (C) 2007 Max Plischke
//  Original Author: Max Plischke <plischke@gmail.com>
//  Link: http://code.google.com/p/open-club-directory/
// 
//  This file is part of the Open-Club-Directory Project.
//
//  Open-Club-Directory is free software: you can redistribute it and/or 
//  modify it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  Open-Club-Directory is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with Open-Club-Directory.
//  If not, see <http://www.gnu.org/licenses/>.
*/

require_once 'setup.php';

function club($id) {
	$query = sprintf("SELECT * FROM clubs where id='%d'", $id);	
	$result = mysql_query($query) or die();
	while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
		$r = array(id => $line[id], name => $line[name], district => $line[district], country => $line[country], city => $line[city], url => $line[url], fb_groupid => $line[fb_groupid], verified => $line[verified]);
	}
	mysql_free_result($result);
	return $r;
}

function clubs($filter='all', $filter_var_id=false, $output='standard', $order='name') {
	if ($filter=='country' || $filter=='district') {
		$where = sprintf("where %s=%d ", $filter, $filter_var_id);
	}
	if ($order=='name') {
		$order_str='name';
	} elseif ($order=='district') {
		$order_str='district, city, name';
	} else {
		$order_str='city, name';
	}
	$query = 'SELECT * FROM clubs '.$where.'order by '.$order_str;
	$result = mysql_query($query) or die();
	while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
		if ($output=='standard') {
			$r[$line[id]] = $line[name].' ('.$GLOBALS[countries][$line[country]].')';		
		} else {
			$r[] = array(id => $line[id], name => $line[name], district => $line[district], country => $line[country], city => $line[city], url => $line[url], fb_groupid => $line[fb_groupid], verified => $line[verified]);
		}
	}
	mysql_free_result($result);
	return $r;
}

function country($club_id) {
	$query = sprintf("SELECT country FROM clubs where id='%d'", $club_id);	
	$result = mysql_query($query) or die();
	$row = mysql_fetch_row($result);
	mysql_free_result($result);
	return $row[0];
}

function countries($filter='default') {
	if ($filter=='inDatabase') {
		$query = 'SELECT country as id FROM clubs GROUP BY country';
		$result = mysql_query($query) or die();
		$GLOBALS[country_number] = mysql_num_rows($result); // for stats
		while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
			$arr[$line[id]] = $GLOBALS[countries][$line[id]];
		}
		mysql_free_result($result);
	} else {
		$arr = $GLOBALS[countries];
	}
	asort($arr);
	return $arr;
}

// positions, countries, etc; groups & other fql arrays need reconfiguration
function dropdown($array, $selected=false) {
	if (count($array)>0) {
		foreach($array as $id => $name) {
			$r .= "\t".'<option value="'.$id.'"'.selected($id, $selected).'>'.$name.'</option>'."\n";
		}
	} else {
		$r = '';
	}
	return $r;
}

function events($handle, $club_var=false, $district_var=false, $country_var=false) {
	// if above are false -> not good in some cases!
	if ($handle == 'personalized') {
    	$club = array();
    	$district = array();
    	$country = array();
	} elseif ($handle == 'country') {
    	$country = array();
    	$districts = array(); // vs. district!
	} elseif ($handle == 'district') {
    	$district = array();
	} elseif ($handle == 'club') {
		$club = array();
	}
	if (!empty($GLOBALS[timezone_GMToffset_sql])) {
		$ts = 'ts>'.$GLOBALS[timezone_GMToffset_sql];
	} else {
		$ts = 'ts>now()';
	}
	if (is_array($club)) {
		$query = sprintf("SELECT * FROM events WHERE host_type='1' && host_id='%d' && %s order by ts", $club_var, $ts);
		$result = mysql_query($query) or die();
		while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
      		$club[$line[e_id]] = $line[host_type];
		}
		mysql_free_result($result);
	}
	if (is_array($district)) {
		$query = sprintf("SELECT * FROM events WHERE host_type='2' && host_id='%d' && %s order by ts", $district_var, $ts);
		$result = mysql_query($query) or die();
		while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
     		$district[$line[e_id]] = $line[host_type];
		}
		mysql_free_result($result);
	}
	if (is_array($country)) {
		$query = sprintf("SELECT * FROM events WHERE host_type='3' && host_id='%d' && %s order by ts", $country_var, $ts);
		$result = mysql_query($query) or die();
		while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
     		$country[$line[e_id]] = $line[host_type];
		}
		mysql_free_result($result);
	}
	if (is_array($districts)) {
		$query = sprintf("SELECT district FROM clubs WHERE country='%d' && district!='0' group by district", $country_var);
		$result = mysql_query($query) or die();
		while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
      		if ($districts_str!='') { $districts_str .= ' or '; }
      		$districts_str .= 'host_id="'.$line[district].'"';
		}
		mysql_free_result($result);
		
		if (isset($districts_str) && $districts_str!='') {
			$query = 'SELECT * FROM events WHERE host_type="2" && ('.$districts_str.') && '.$ts.' order by ts';
			$result = mysql_query($query) or die();
			while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
      			$districts[$line[e_id]] = $line[host_type];
			}
			mysql_free_result($result);
		}
	}
	$events = array_merge((array)$club, (array)$district, (array)$country, (array)$districts); // typecasting
    if (count($events)==0) { $events = false; }
    return $events;
}

function feed($story, $member) {
	global $_ClubName;
	if ($story=='club') {
		$title_template = '{actor} joined '.$_ClubName.' Club of {club}';
		$title_data = '{"club":"<a href=\"'.$appcallbackurl_json.'?club='.$member[club_id].'\">'.$member[club_name].'<\/a>"}';
		$body_template = '';
		$body_data = '';
		return array(title_template=>$title_template, title_data=>$title_data, body_template=>$body_template, body_data=>$body_data);
	} elseif ($story=='position') {
		$positions_arr = array(1 => 'President', 2 => 'Vice President', 12 => 'Past President', 3 => 'Secretary', 4 => 'Treasurer', 5 => 'Service Committee Member', 6 => 'Community Service Committee Member', 7 => 'International Service Committee Member', 8 => 'Professional Development Committee Member', 11 => 'Finance Committee Member', 9 => 'Member', 10 => 'Guest');
		$positions_plural_arr = array(1 => 'Presidents', 2 => 'Vice Presidents', 12 => 'Past Presidents', 3 => 'Secretaries', 4 => 'Treasurers', 5 => 'Service Committee Members', 6 => 'Community Service Committee Members', 7 => 'International Service Committee Members', 8 => 'Professional Development Committee Members', 11 => 'Finance Committee Members', 9 => 'Members', 10 => 'Guests');
		$query = sprintf("SELECT count(distinct(fb_id)) as count FROM users where position=%d group by position", $member[position]);
		$result = mysql_query($query) or die();
		while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
			$num = $row[count];
		}
		$title_template = '{actor} is a '.$_ClubName.' Club {position}!';
		$title_data = '{"position":"'.$positions_arr[$member[position]].'"}';
		$body_template = 'There are currently {num} '.$_ClubName.' Club {position}. {act}';
		$body_data = '{"num":'.$num.', "position":"'.$positions_plural_arr[$member[position]].'", "act":"<a href=\"'.$appcallbackurl_json.'edit.php\">What are you?<\/a>"}';
		return array(title_template=>$title_template, title_data=>$title_data, body_template=>$body_template, body_data=>$body_data);
	}
} 

function member($user) {
	$query = sprintf("SELECT users.fb_id as fb_id, users.position as position, users.name as name, users.ts as ts, users.verified as verified, users.changes as changes, clubs.id as club_id, clubs.name as club_name, clubs.district as club_district, clubs.country as club_country FROM users, clubs where clubs.id=users.club_id && users.fb_id=%d", $user);
	$result = mysql_query($query) or die();
	if (mysql_num_rows($result) != 0) {
		$r = mysql_fetch_array($result, MYSQL_ASSOC);
	} else {
		$r=false;
	}
	mysql_free_result($result);
	return $r;
}

function members($filter='all', $filter_var_id, $offset=false, $row_count=false, $order='default', $extended=true) {
	if ($filter=='club') {
		$extended=false;
	} elseif ($filter=='country') {
		$extended=true;
	}
	if ($extended==true) {
		if ($filter=='country') {
			$where = sprintf(", clubs WHERE clubs.id=users.club_id AND clubs.%s=%d", $filter, $filter_var_id);
		} else {
			$where = ', clubs WHERE clubs.id=users.club_id';
		}
		$extended_fields = ', clubs.id as club_id, clubs.name as club_name, clubs.city as club_city, clubs.country as club_country';
	} else {
		if ($filter=='club') {
			$where = sprintf(" WHERE users.club_id=%d ", $filter_var_id);
		}
	}
	if ($order=='default') {
		$order_str=' order by users.ts desc';
	} else {
		$order_str='';
	}
	if (!empty($row_count)) {
		$limit = sprintf(" LIMIT %d, %d ", $offset, $row_count);
	}
	// exclude guests
	if (empty($where)) { $where .= ' WHERE '; } else { $where .= ' AND '; }
	$where .= 'users.position!=10';
	
	$query = 'SELECT users.fb_id as fb_id, users.position as position'.$extended_fields.' FROM users'.$where.$order_str.$limit;
	$result = mysql_query($query) or die();
	while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
      	if ($extended==true) {
      		$r[] = array(fb_id => $line[fb_id], position => $line[position], club_id => $line[club_id], club_name => $line[club_name], club_city => $line[club_city], club_country => $line[club_country]);
      	} else {
      		$r[] = array(fb_id => $line[fb_id], position => $line[position]);
      	}
	}
	mysql_free_result($result);
	if (count($r)==0) { return false; }
	return $r;
}

function positive_number($nr) {
	if(ereg("^[0-9]+$", $nr) && $nr > 0){
		return true;
	} else {
		return false;
	}
}

function profile($member) {
	global $_ClubName, $appcallbackurl, $_url_logo;
	if ($member!=false) {
		$position = $GLOBALS[positions_incl_arr][$member[position]];		
		$profile_fbml = '<table><tr align="left" valign="middle"><td><img src="'.$_url_logo.'" alt=""></td><td width="99%"><fb:name uid="'.$member[fb_id].'" firstnameonly="true" useyou="false" linked="no" /> is '.$position.' of the <a href="'.$appcallbackurl.'?club='.$member[club_id].'">'.$_ClubName.' Club of '.$member[club_name].'</a>.</td></tr></table>';
	} else {
		$profile_fbml = '<table><tr align="left" valign="middle"><td><img src="'.$_url_logo.'" alt=""></td><td width="99%"><a href="'.$appcallbackurl.'">'.$_ClubName.' Clubs and Events</a> in your Region. Are you a <a href="'.$appcallbackurl.'add.php">Member?</a></td></tr></table>';
	}
	return $profile_fbml;
}

function selected($club_id, $selected) {
	if ($club_id == $selected) {
		return ' selected';
	}
}

function security($changes_done) {
	$allowed=3;
	if ($changes_done>$allowed) {
		echo 'You have changed your club more than '.$allowed.' times. Please contact the admin.';
		// send mail to me
		exit;
	}
	return ($changes_done+1);
}

$GLOBALS[positions_arr] = array(1 => 'President', 2 => 'Vice President', 12 => 'Past President', 3 => 'Secretary', 4 => 'Treasurer', 5 => 'Club Service Committee', 6 => 'Community Service Committee', 7 => 'International Service Committee', 8 => 'Professional Development Committee', 11 => 'Finance Committee', 9 => 'Member', 10 => 'Guest');

$GLOBALS[positions_incl_arr] = array(1 => 'president', 12 => 'past president', 2 => 'vice president', 3 => 'secretary', 4 => 'treasurer', 5 => 'in the club service committee', 6 => 'in the community service committee', 7 => 'in the international service committee', 8 => 'in the professional development committee', 9 => 'a member', 10 => 'a guest', 11 => 'in the finance committee');

$GLOBALS[countries] = array(398 => 'United States', 326 => 'Canada', 327 => 'England', 228 => 'Scotland', 288 => 'Wales', 302 => 'Abuja', 233 => 'Afghanistan', 399 => 'Akrotiri', 320 => 'Albania', 400 => 'Algeria', 221 => 'American Samoa', 401 => 'Andorra', 234 => 'Angola', 402 => 'Anguilla', 403 => 'Antarctica', 278 => 'Antigua', 190 => 'APO', 135 => 'Argentina', 404 => 'Armenia', 284 => 'Aruba', 405 => 'Ashmore and Cartier Islands', 111 => 'Australia', 81 => 'Austria', 213 => 'Azerbaijan', 174 => 'Bahrain', 165 => 'Bangladesh', 208 => 'Barbados', 112 => 'Belarus', 72 => 'Belgium', 176 => 'Belize', 406 => 'Benin', 181 => 'Bermuda', 407 => 'Bhutan', 132 => 'Bolivia', 236 => 'Bosnia and Herzegovina', 177 => 'Botswana', 122 => 'Brazil', 290 => 'British Virgin Islands', 144 => 'Brunei', 369 => 'Bulgaria', 235 => 'Burkina Faso', 385 => 'Burundi', 188 => 'Cambodia', 209 => 'Cameroon', 215 => 'Cape Verde', 211 => 'Cayman Islands', 470 => 'Côte d\'Ivoire', 408 => 'Central African Republic', 409 => 'Chad', 468 => 'Channel Islands', 118 => 'Chile', 91 => 'China', 310 => 'Colombia', 410 => 'Comoros', 162 => 'Costa Rica', 297 => 'Croatia', 227 => 'Cuba', 102 => 'Curacao', 106 => 'Cyprus', 175 => 'Czech Republic', 411 => 'Democratic Republic Congo', 99 => 'Denmark', 412 => 'Djibouti', 287 => 'Dominica', 145 => 'Dominican Republic', 413 => 'East Timor', 160 => 'Ecuador', 121 => 'Egypt', 195 => 'El Salvador', 414 => 'Equatorial Guinea', 395 => 'Eritrea', 466 => 'Estonia', 168 => 'Ethiopia', 416 => 'Falkland Islands', 417 => 'Faroe Islands', 464 => 'Federated States of Micronesia', 418 => 'Fiji', 200 => 'Finland', 84 => 'France', 419 => 'French Guiana', 420 => 'French Polynesia', 312 => 'Gabon', 368 => 'Georgia', 79 => 'Germany', 85 => 'Ghana', 422 => 'Gibraltar', 128 => 'Greece', 423 => 'Greenland', 390 => 'Grenada', 65 => 'Guam', 138 => 'Guatemala', 424 => 'Guinea', 425 => 'Guinea-Bissau', 170 => 'Guyana', 226 => 'Haiti', 169 => 'Honduras', 69 => 'Hong Kong', 153 => 'Hungary', 285 => 'Iceland', 68 => 'India', 166 => 'Indonesia', 232 => 'Iran', 231 => 'Iraq', 178 => 'Ireland', 469 => 'Isle Of Man', 136 => 'Israel', 86 => 'Italy', 77 => 'Jamaica', 75 => 'Japan', 146 => 'Jordan', 87 => 'Kazakhstan', 67 => 'Kenya', 426 => 'Kiribati', 110 => 'Kuwait', 427 => 'Kyrgyzstan', 472 => 'Laos', 281 => 'Latvia', 98 => 'Lebanon', 428 => 'Lesotho', 429 => 'Liberia', 430 => 'Libya', 431 => 'Liechtenstein', 271 => 'Lithuania', 183 => 'Luxembourg', 432 => 'Macau', 197 => 'Macedonia', 433 => 'Madagascar', 251 => 'Malawi', 103 => 'Malaysia', 434 => 'Maldives', 435 => 'Mali', 436 => 'Malta', 437 => 'Marshall Islands', 438 => 'Martinique', 439 => 'Mauritania', 143 => 'Mauritius', 109 => 'Mexico', 294 => 'Moldova', 204 => 'Monaco', 440 => 'Mongolia', 473 => 'Montenegro', 74 => 'Morocco', 441 => 'Mozambique', 94 => 'Myanmar', 229 => 'Namibia', 442 => 'Nauru', 120 => 'Nepal', 147 => 'Netherlands', 372 => 'Netherlands Antilles', 90 => 'New Zealand', 105 => 'Nicaragua', 393 => 'Niger', 149 => 'Nigeria', 444 => 'North Korea', 217 => 'Northern Ireland', 182 => 'Northern Mariana Islands', 76 => 'Norway', 164 => 'Oman', 141 => 'Pakistan', 358 => 'Palau', 421 => 'Palestine', 124 => 'Panama', 311 => 'Papua New Guinea', 203 => 'Paraguay', 95 => 'Peru', 83 => 'Philippines', 89 => 'Poland', 142 => 'Portugal', 154 => 'Qatar', 445 => 'Republic of the Congo', 119 => 'Romania', 78 => 'Russia', 446 => 'Rwanda', 447 => 'Saint Kitts and Nevis', 448 => 'Saint Vincent and the Grenadines', 449 => 'Samoa', 450 => 'San Marino', 451 => 'Sao Tome and Principe', 104 => 'Saudi Arabia', 201 => 'Senegal', 286 => 'Serbia', 452 => 'Seychelles', 453 => 'Sierra Leone', 70 => 'Singapore', 212 => 'Slovakia', 295 => 'Slovenia', 454 => 'Solomon Islands', 455 => 'Somalia', 161 => 'South Africa', 88 => 'South Korea', 114 => 'Spain', 158 => 'Sri Lanka', 364 => 'St. Lucia', 299 => 'Sudan', 456 => 'Suriname', 194 => 'Swaziland', 148 => 'Sweden', 80 => 'Switzerland', 163 => 'Syria', 97 => 'Taiwan', 397 => 'Tajikistan', 155 => 'Tanzania', 73 => 'Thailand', 130 => 'The Bahamas', 303 => 'The Gambia', 457 => 'Togo', 458 => 'Tonga', 167 => 'Trinidad and Tobago', 139 => 'Tunisia', 82 => 'Turkey', 459 => 'Turkmenistan', 460 => 'Tuvalu', 202 => 'Uganda', 219 => 'Ukraine', 123 => 'United Arab Emirates', 159 => 'Uruguay', 131 => 'US Virgin Islands', 113 => 'Uzbekistan', 461 => 'Vanuatu', 462 => 'Vatican City', 100 => 'Venezuela', 186 => 'Vietnam', 463 => 'Western Sahara', 206 => 'Yemen', 192 => 'Zambia', 151 => 'Zimbabwe');

?>